import AbTestMgr from "./AbTestMgr";
import r from "./MatchGameData"
import n from "./AbTestMgr"
import l from "./PeripheralSystemDataMgr"
import s from "./PlayerDataMgr"
import c from "./EventMgr"
import u from "./GameEventType"
import p from "./frameworkManager"
import d from "./AdManager"
import h from "./ClientData"
import f from "./SdkHelper"
import g from "./SystemConfig"
import y from "./EngineUtil"

const { ccclass, property } = cc._decorator;

@ccclass
class PlayerDataSys {
    @property(cc.Integer)
    cash_balance: number = 0;

    @property(cc.Integer)
    gold_balance: number = 0;

    @property(cc.Integer)
    total_login_days: number = 0;

    @property(cc.Integer)
    total_sign_days: number = 0;

    @property(cc.Integer)
    money_balance: number = 0;

    @property(cc.Integer)
    smile: number = 0;

    @property(cc.Integer)
    user_level: number = 0;

    @property(cc.Integer)
    user_level_advance: number = 0;

    @property(cc.Boolean)
    reco_switch: boolean = false;

    @property(cc.String)
    wx_head: string = "";

    @property(cc.Integer)
    is_reviewer: number = 0;

    @property(cc.Integer)
    new_add_flag: number = 0;


    public user_id = "";
    public user_name = "";
    public yid = "";
    public wx_gender = "";
    public bind_wx = 1;


    private static _instance: PlayerDataSys = null;

    public static getInstance(): PlayerDataSys {
        if (!this._instance) {
            this._instance = new PlayerDataSys();
        }
        return this._instance;
    }

    public init(data: any): void {
        if (data) {
            console.log("PlayerDataSys data: ", JSON.stringify(data));
            
            const { user_info, game_info, ab_info } = data;
            this.setUserInfo(user_info);
            this.setGameInfo(game_info);
            if (ab_info) {
                AbTestMgr.init(ab_info);
            }
        }
    }

    public initUserId(data: any): void {
        console.log("initUserId data: ", data);
        const { user_id, user_name, yid } = data;
        this.user_id = user_id || "";
        this.user_name = user_name || "";
        this.yid = yid || "yid_read_failed";
        h.yid = yid;
        h.setCommonData();
        y.setLocalData("yid", yid);
    }

    public initWxData(data: any): void {
        if (data) {
            this.initUserId(data);
            const { gender, yid, user_id, nickname, headimgurl } = data;
            this.yid = yid || "yid_read_failed";
            this.user_id = user_id;
            this.user_name = nickname;
            this.wx_gender = gender || "";
            this.wx_head = headimgurl || "";
            this.bind_wx = 1;
        }
    }

    public create_time;
    public day_video_count;
    public setUserInfo(data: any): void {
        const { not_tf, is_reviewer, cash_balance, create_time, day_video_count, gold_balance, is_login, total_login_days, total_sign_days, money_balance, smile, user_level, user_level_advance, reco_switch, headimgurl } = data;
        this.cash_balance = cash_balance || 0;
        this.create_time = create_time;
        this.day_video_count = day_video_count || 0;
        this.total_login_days = total_login_days || 0;
        this.total_sign_days = total_sign_days || 0;
        this.money_balance = money_balance || 0;
        this.smile = smile || 0;
        this.user_level = user_level || 0;
        this.user_level_advance = user_level_advance || 0;
        this.reco_switch = reco_switch;
        this.wx_head = headimgurl || "";
        this.is_reviewer = is_reviewer || 0;
        this.new_add_flag = not_tf || 0;

        f.setUserInfo({
            yid: this.yid,
            user_id: this.user_id,
            gender: this.wx_gender,
            create_time: this.create_time,
            is_travel: 0
        });
    }

    public setGameInfo(data: any): void {
        const { game_level } = data;
        l.setPeripheralSystemInfo(data);
        r._gameLevel = game_level;
    }

    public getUserCpm(): object {
        return d.getInstance().cpm_data || {
            cpm: 0,
            source: "",
            unitId: "",
            isApp: "",
            isClose: "",
            activity_date: "",
            activity_num: ""
        };
    }

    public getMoneyBalance(e: number, t: string = "元"): string {
        let balance = e || this.money_balance || 0;
        return `${Math.floor((balance / g.MONEY_PRO) * 100) / 100}${t}`;
    }

    public updateCashBalace(e: number, t: boolean = true): void {
        if (e) {
            if (t) {
                this.cash_balance = e;
            } else {
                this.cash_balance = e;
            }
            c.trigger(u.UPDATE_USER_CASH, {
                reward: e,
                isAdd: t
            });
        }
    }

    public updateGoldBalace(e: number): void {
        if (e) {
            this.gold_balance = e;
            c.trigger(u.UPDATE_USER_GOLD, {
                reward: e
            });
        }
    }
}
export default PlayerDataSys.getInstance();